﻿#include "AudioManager.h"

AudioManager AudioManager::instance;

// 释放所有资源
bool AudioManager::releaseAll() {
    std::vector<std::string> keys;
    for (auto it = audios.begin(); it != audios.end(); it++) {
        keys.push_back(it->first);
    }
    for (auto id : keys) {
        unload(id);
    }
    return true;
}

// 加载
bool AudioManager::load(const std::string id, const std::string path) {
    //删除name中的空白字符
    std::string name = id;
    name.erase(std::remove_if(name.begin(), name.end(), [](unsigned char c) {
        return std::isspace(c);
        }), name.end());
    if (name != id) {
        std::cerr << ("Warning: The name \"" + id + "\" is invalid. The name has been transformed into \"" + name + "\".") << std::endl;
    }
	if (AudioPlayer::includeID(name)) {
        std::cerr << ("Error: The name \"" + name + "\" already exists. Loading failed.") << std::endl;
        return false;
	}
    audios[name] = std::make_shared<AudioPlayer>();
    //audios[name] = AudioPlayer::create();
    if (audios[name]->init(path, name)) {
        return true;
    }
    else {
        audios[name]=nullptr;
        audios.erase(name);
        std::cerr << ("Error: " + name + " loading failed.") << std::endl;
        return false;
    }
}
bool AudioManager::unload(const std::string id) {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    audios[id]=nullptr;
    audios.erase(id);
    return true;
}

// 播放控制方法
//开始播放
bool AudioManager::play(const std::string id) const{
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->play();
    
}
//暂停
bool AudioManager::pause(const std::string id) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->pause();
}
//重新开始
bool AudioManager::restart(const std::string id) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->restart();
}
//设置循环播放
bool AudioManager::setLoop(const std::string id,bool loop) {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->setLoop(loop);
}

// 音量控制方法
//设置音量
bool AudioManager::setVolume(const std::string id,float volume) {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->setVolume(volume);
}
//获取音量
float AudioManager::getVolume(const std::string id) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return 0;
    }

    return audios.at(id)->getVolume();
}

// 淡入淡出控制方法
// 淡入
bool AudioManager::fadeIn(const std::string id,float durationSeconds) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->fadeIn(durationSeconds);
}
//淡出
bool AudioManager::fadeOut(const std::string id,float durationSeconds) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->fadeOut(durationSeconds);

}

// 状态查询方法
//检查是否成功初始化
bool AudioManager::isInitialized(const std::string id) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->isInitialized();
}
//检查是否正在播放
bool AudioManager::isPlaying(const std::string id) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->isPlaying();
}
//检查是否循环播放
bool AudioManager::isLooping(const std::string id) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return false;
    }
    return audios.at(id)->isLooping();
}
//获取音频总时长
float AudioManager::getDuration(const std::string id) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return 0;
    }
    return audios.at(id)->getDuration();
}
//获取当前播放位置
float AudioManager::getPlayingOffset(const std::string id) const {
    if (!AudioPlayer::includeID(id)) {
        std::cerr << ("Error: This id \"" + id + "\" does no exist.") << std::endl;
        return 0;
    }
    return audios.at(id)->getPlayingOffset();
}